package jianzhiOffer;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

//剑指 Offer 32 - I. 从上到下打印二叉树
public class Num32_I_levelOrder {
    public int[] levelOrder(TreeNode root) {
        List<Integer> ret = new ArrayList<>();
        if (root == null){
            return new int[0];
        }
        Deque<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                ret.add(node.val);
                if (node.left != null){
                    queue.offer(node.left);
                }
                if (node.right != null){
                    queue.offer(node.right);
                }
            }


        }
        int [] ans = new int[ret.size()];
        for (int i = 0; i < ans.length; i++) {
            ans[i] = ret.get(i);
        }
        return ans;






    }

}
